/**
 * @author:albert
 * @date:2016年7月1日
 */
package com.bkbw.live.battle.bean;

import java.util.HashMap;
import java.util.Map;

import org.apache.mina.core.session.IoSession;

/**
 * @author albert
 *
 */
public class CharManager {
	public static class CharacterInfo {
		private int charId;
		private IoSession ioSession;
		public int getCharId() {
			return charId;
		}
		public IoSession getIoSession() {
			return ioSession;
		}
		
		private int tableId;
		
		
		public int getTableId() {
			return tableId;
		}
		public void setTableId(int tableId) {
			this.tableId = tableId;
		}
		public CharacterInfo(int charId, IoSession ioSession) {
			this.charId = charId;
			this.ioSession = ioSession;
		}
	}
	
	private static CharManager manager = new CharManager();
	public static CharManager getInstance() {
		return manager;
	}
	
	private Map<Integer, CharacterInfo> characterMap = new HashMap<Integer, CharManager.CharacterInfo>();
	private Map<IoSession, CharacterInfo> charSessionMap = new HashMap<IoSession, CharManager.CharacterInfo>();
	
	public boolean hasCharacter(int charId) {
		if (characterMap.containsKey(charId)) {
			return true;
		}
		else {
			return false;
		}
	}
	
	public void addCharacter(int charId, IoSession ioSession) {
		CharacterInfo characterInfo = new CharacterInfo(charId, ioSession);
		characterMap.put(charId, characterInfo);
		charSessionMap.put(ioSession, characterInfo);
	}
	
	public void delCharacter(int charId) {
		if (characterMap.containsKey(charId)) {
			charSessionMap.remove(characterMap.get(charId).getIoSession());
			characterMap.remove(charId);
		}
	}
	
	public CharacterInfo getCharacter(int charId) {
		return characterMap.get(charId);
	}
	
	public CharacterInfo getCharacter(IoSession ioSession) {
		return charSessionMap.get(ioSession);
	}
	
}
